//
//  MessagesScreen.m
//  ServiceApp
//
//  Created by Training Camp on 12/17/14.
//  Copyright (c) 2014 TrainingCamp. All rights reserved.
//

#import "MessagesScreen.h"
#import "NotificationManager.h"
#import "Notification.h"

@interface MessagesScreen ()

@property Notification *selectedNote;

@end

@implementation MessagesScreen

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[[[self.tabBarController tabBar]items]objectAtIndex:0]setEnabled:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Messages";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(update:) name:@"NewNotification" object:nil];
    //Disable swipe to go back gesture
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [[NotificationManager sharedNotifications] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoteCell" forIndexPath:indexPath];
    
    Notification* note = [[NotificationManager sharedNotifications] noteAtIndex:indexPath.row];
    // Configure the cell...
    
    cell.textLabel.text = note.text;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Table: %ld", (long)note.tableId];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Notification* note = [[NotificationManager sharedNotifications] noteAtIndex:indexPath.row];
    self.selectedNote = note;
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Accepting Message"
                                                    message:@"Please complete task ASAP"
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:@"CANCEL", nil];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0){
        [[NotificationManager sharedNotifications] makeMessageDone:self.selectedNote];
        [self.tableView reloadData];
    }
    else
    {
        //NSLog(@"cancel");
    }
}

- (void) update:(NSNotification*) note{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}

@end
